package com.augn.service.impl;

import com.augn.entity.CulturalHeritage;
import com.augn.entity.CulturalHeritage;
import com.augn.entity.Result;
import com.augn.mapper.CulturalHeritageMapper;
import com.augn.service.ICulturalHeritageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class CulturalHeritageServiceImpl extends ServiceImpl<CulturalHeritageMapper, CulturalHeritage> implements ICulturalHeritageService {

    @Autowired
    private CulturalHeritageMapper mapper;

    // 新增一个文化遗产
    @Override
    public Result<?> insertOneCulturalHeritage(CulturalHeritage culturalHeritage) {
        int insert = mapper.insert(culturalHeritage);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    // 删除一个文化遗产
    @Override
    public Result<?> deleteOneCulturalHeritage(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    // 更新一个文化遗产
    @Override
    public Result<?> updateOneCulturalHeritage(CulturalHeritage culturalHeritage) {
        int update = mapper.updateById(culturalHeritage);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    // 获取一个文化遗产
    @Override
    public Result<?> getOneCulturalHeritage(Integer id) {
        CulturalHeritage culturalHeritage = mapper.selectById(id);
        if(culturalHeritage==null){
            return Result.error("-1","无此文化遗产");
        } else{
            return Result.success(culturalHeritage);
        }
    }

    // 获取符合条件的文化遗产列表
    @Override
    public Result<?> getPartCulturalHeritage(CulturalHeritage culturalHeritage, Integer pageNum, Integer pageSize) {
        QueryWrapper<CulturalHeritage> wrapper= new QueryWrapper<>();
        if(culturalHeritage.getInheritorsId()!=null){
            wrapper.eq("inheritors_id",culturalHeritage.getInheritorsId());
        }
        if (culturalHeritage.getCity()!=null) {
            wrapper.like("city",culturalHeritage.getCity());
        }
        if (culturalHeritage.getSort()!=null) {
            wrapper.like("sort",culturalHeritage.getSort());
        }
        if (culturalHeritage.getReleaseTime()!=null) {
            wrapper.like("release_time",culturalHeritage.getReleaseTime());
        }
        if (culturalHeritage.getDetail()!=null) {
            wrapper.and(i ->i.like("name",culturalHeritage.getDetail())
                    .or().like("city",culturalHeritage.getDetail())
                    .or().like("sort",culturalHeritage.getDetail())
                    .or().like("release_time",culturalHeritage.getDetail())
                    .or().like("detail",culturalHeritage.getDetail()));
        }

        long pages ,total;
        List<CulturalHeritage> culturalHeritageList;
        Map<String,Object> ret = new HashMap<>();
        if((pageNum==null&&pageSize==null)||(pageNum==null&&pageSize!=null)){
            culturalHeritageList =  mapper.selectList(wrapper);
            ret.put("culturalHeritageList",culturalHeritageList);
        } else {
            if(pageSize==null){
                pageSize=20;
            }
            Page<CulturalHeritage> culturalHeritagePage = new Page<>(pageNum , pageSize);
            IPage<CulturalHeritage> culturalHeritageIPage = mapper.selectPage(culturalHeritagePage , wrapper);
            culturalHeritageList = culturalHeritageIPage.getRecords();
            pages = culturalHeritageIPage.getPages();
            total = culturalHeritageIPage.getTotal();
            ret.put("culturalHeritageList",culturalHeritageList);
            ret.put("pages",pages);
            ret.put("total",total);
        }

        if(culturalHeritageList.isEmpty()){
            return Result.error("-1","没有符合此条件的文化遗产");
        } else{
            return Result.success(ret);
        }
    }
}
